/*
 * GET home page.
 */
var crypto = require('crypto');
var User = require('../modules/user.js');
var Post = require('../modules/post.js');

module.exports = function(app) {
	app.get('/', function(req, res) {
		Post.get(null, function(err, posts) {
			if (err) {
				posts = [];
			};
			res.render('index', {
				title: 'INDEX',
				posts: posts
			});
		});
	});

	app.get('/reg', checkNotLogin);
	app.get('/reg', function(req, res) {
		res.render('reg', {
			title: 'REG'
		});
	});

	app.post('/reg', checkNotLogin);
	app.post('/reg', function(req, res) {
		if(req.body['username'].length < 6 || req.body['username'].length > 9){
			req.flash('error','the username must greater six and less than ten')
			return res.redirect('/reg');
		}

		if(req.body['password'].length < 6 || req.body['password'].length > 9){
			req.flash('error','the password must greater six and less than ten')
			return res.redirect('/reg');
		}

		if (req.body['password-repeat'] != req.body['password']) {
			req.flash('error', 'the passwords are not same!');
			return res.redirect('/reg');
		};
		var md5 = crypto.createHash('md5');
		var password = md5.update(req.body.password).digest('base64');

		var newUser = new User({
			name: req.body.username,
			password: password,
		});
		User.get(newUser.name, function(err, user) {
			if (user) {
				err = "User already exists.";
			}
			if (err) {
				req.flash('error', err);
				return res.redirect('/reg');
			}
			newUser.save(function(err) {
				if (err) {
					req.flash('error', err);
					return res.redirect('/reg');
				}
				req.session.user = newUser;
				req.flash('success', 'reg success');
				return res.redirect('/');
			});
		});
	});

	app.get('/login', checkNotLogin);
	app.get('/login', function(req, res) {
		res.render('login', {
			title: 'user login',
		});
	});

	app.post('/login', checkNotLogin);
	app.post('/login', function(req, res) {
		var md5 = crypto.createHash('md5');
		var password = md5.update(req.body.password).digest('base64');

		User.get(req.body.username, function(err, user) {
			if (!user) {
				req.flash('error', 'user is not exists');
				return res.redirect('/login');
			}
			if (user.password != password) {
				req.flash('error', 'password is error');
				return res.redirect('/login');
			}
			req.session.user = user;
			req.flash('success', 'login success');
			res.redirect('/');
		});
	});

	app.get('/logout', checkLogin);
	app.get('/logout', function(req, res) {
		req.session.user = null;
		req.flash('success', 'logout success');
		res.redirect('/');
	});

	app.post('/post', checkLogin);
	app.post('/post', function(req, res) {

		if(req.body['post'].length < 5 || req.body['post'].length > 200){
			req.flash('error','ur push words must between five and two hundred.')
			return res.redirect('/post')
		}

		var currentUser = req.session.user;
		var post = new Post(currentUser.name, req.body.post);
		post.save(function(err) {
			if (err) {
				req.flash('error', err);
				return res.redirect('/');
			}
			req.flash('success', 'push success!');
			res.redirect('/' + currentUser.name);
		});
	});

	app.get('/:user', function(req, res) {
		User.get(req.params.user, function(err, user) {
			if (!user) {
				req.flash('error', 'user is not exists');
				return res.redirect('/');
			}
			Post.get(user.name, function(err, posts) {
				if (err) {
					req.flash('error', err);
					return res.redirect('/');
				}
				res.render('user', {
					title: user.name,
					posts: posts,
				});
			});
		});
	});
};

function checkLogin(req, res, next) {
	if (!req.session.user) {
		req.flash('error', 'not login');
		return res.redirect('/login');
	}
	next();
}

function checkNotLogin(req, res, next) {
	if (req.session.user) {
		req.flash('error', 'already logined');
		return res.redirect('/');
	}
	next();
}